package com.snow4cloud.common.datasource.config;

import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration;
import com.snow4cloud.common.datasource.property.DataSourceProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;

/**
 * 单个数据源配置
 *
 * @Description
 * @Author fanxl
 * @Date 2021/5/11
 **/
@Slf4j
@Configuration
@AllArgsConstructor
@AutoConfigureBefore(DynamicDataSourceAutoConfiguration.class)
@EnableConfigurationProperties(DataSourceProperties.class)
public class SnowDataSourceAutoConfiguration {

    private DataSourceProperties properties;

    private StringEncryptor stringEncryptor;

    @Bean
    @ConditionalOnMissingBean({DynamicDataSourceProvider.class})
    public DynamicDataSourceProvider dynamicDataSourceProvider() {
        properties.setIsDynamic(false);
        return new JdbcDynamicDataSourceProvider(stringEncryptor, properties);
    }

}
